package persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;


import negocio.Inconsistencia;

public class perInconsistencia {
	//private Inconsistencia i;
	private int a;
	//private Connection con;
	private ResultSet rs;
	public void insert(Inconsistencia i) {
		try {
			Connection con=PoolConnection.getPoolConnection().getConnection();
			PreparedStatement ps2=con.prepareStatement("select max(IDINCONSISTENCIA) from INCONSISTENCIAS");
			ResultSet rs2=ps2.executeQuery();		
			if(rs2.next())
			{
				a=rs2.getInt(1);
				PreparedStatement ps = con.prepareStatement("insert into INCONSISTENCIAS values(?,?,?,?,?)");		
				ps.setInt(1, ++a);
				ps.setInt(2, i.getOs());
				ps.setString(3, i.getDescripcion());
				ps.setString(4, i.getMotivo());
				ps.setDate(5, (Date) i.getFecha());
				ps.execute();
			}			
			PoolConnection.getPoolConnection().releaseConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public Vector<Inconsistencia> select(int os)
	{
		Connection con=PoolConnection.getPoolConnection().getConnection();
		Vector <Inconsistencia> v=new Vector <Inconsistencia>();
		try {
			PreparedStatement ps = con.prepareStatement("select * from INCONSISTENCIAS where OS=?");
			ps.setInt(1, os);
			rs=ps.executeQuery();
			while(rs.next())
			{
				Inconsistencia i=new Inconsistencia();
				i.setNroInconsistencia(rs.getInt(1));
				i.setOs(os);
				i.setDescripcion(rs.getString(3));
				i.setMotivo(rs.getString(4));
				v.add(i);
			}
			PoolConnection.getPoolConnection().releaseConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return v;
		
	}
	public Vector <Inconsistencia> select (String desde,String hasta)
	{
		Connection con=PoolConnection.getPoolConnection().getConnection();
		Vector <Inconsistencia> v=new Vector <Inconsistencia>();
		try {
			PreparedStatement ps = con.prepareStatement("select * from INCONSISTENCIAS where fecha between ? and ?");
			ps.setString(1, desde);
			ps.setString(2, hasta);
			rs=ps.executeQuery();
			while(rs.next())
			{
				Inconsistencia i=new Inconsistencia();
				i.setNroInconsistencia(rs.getInt(1));
				i.setOs(rs.getInt(2));
				i.setDescripcion(rs.getString(3));
				i.setMotivo(rs.getString(4));
				v.add(i);
			}
			PoolConnection.getPoolConnection().releaseConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return v;
		
	}
}
